package com.cff.action.imggps;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

import com.cff.util.FileUtil;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.GpsDirectory;

import net.sf.json.JSONObject;

public class GpsImgOriActionListener extends GpsImgActionListener {
	private JFrame tmpFrame;
	private JTextArea lastJTextArea;
	private static Properties confPro = null;
	public GpsImgOriActionListener(JFrame tmpFrame) {
		super();
		this.tmpFrame = tmpFrame;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		confPro = FileUtil.getInstance();
		System.out.println(getFilePath());
		if (lastJTextArea != null) {
			tmpFrame.remove(lastJTextArea);
		}
		String res = "gps信息查询失败。";
		File jpegFile = new File(getFilePath());
		Metadata metadata = null;
		try {
			metadata = JpegMetadataReader.readMetadata(jpegFile);
		} catch (JpegProcessingException | IOException e2) {
			e2.printStackTrace();
		}
		if (metadata != null) {
			GpsDirectory gps = metadata.getFirstDirectoryOfType(GpsDirectory.class);
			if (gps != null) {
				String longitude = gps.getDescription(GpsDirectory.TAG_LONGITUDE);
				String latitude = gps.getDescription(GpsDirectory.TAG_LATITUDE);
				String longitudeRef = gps.getDescription(GpsDirectory.TAG_LONGITUDE_REF);
				String latitudeRef = gps.getDescription(GpsDirectory.TAG_LATITUDE_REF);
				System.out.println(longitude);
				System.out.println(longitudeRef);
				System.out.println(latitude);
				System.out.println(latitudeRef);

				double longitudeloc = convertLocation(longitude);
				double latitudeloc = convertLocation(latitude);
				System.out.println(longitudeloc);
				System.out.println(latitudeloc);
				String location = latitudeloc + "," + longitudeloc;
				HttpClient client = new HttpClient();
				String url = "http://api.map.baidu.com/geocoder/v2/?coordtype=wgs84ll" + "&location=" + location
						+ "&output=json&pois=0&" + "ak="+confPro.getProperty("baiduAk");
				GetMethod getMethod = new GetMethod(url);
				res = "网络异常，请重试！";
				try {
					client.executeMethod(getMethod);
					System.out.println(getMethod.getStatusLine()); // 打印结果页面
					res = getMethod.getResponseBodyAsString();
					System.out.println(res);
					JSONObject jsonObj = JSONObject.fromObject(res);
					JSONObject addr = JSONObject.fromObject(jsonObj.get("result"));
					res = addr.get("formatted_address") + ":" + addr.get("business") + "("
							+ addr.get("sematic_description") + ")";

				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}

		} else {
			res = "该图片无gps信息，可能是图片类型不对或已被处理过。";
		}
		
		JTextArea resultLabel = new JTextArea();
		resultLabel.setSelectedTextColor(Color.RED);
		resultLabel.setLineWrap(true);        //激活自动换行功能 
		resultLabel.setWrapStyleWord(true);        
		resultLabel.setBounds(tmpFrame.getWidth()/2+30, 100, tmpFrame.getWidth()/2-100, tmpFrame.getHeight()-100);;
		resultLabel.setText(res);
		tmpFrame.add(resultLabel);
        tmpFrame.pack();
        lastJTextArea = resultLabel;

        setToDefault(tmpFrame);
        tmpFrame.setVisible(true);
	}

	public static double convertLocation(String loc) {
		String locArr[] = loc.split(" ");
		double location = Double.valueOf(locArr[0].substring(0, locArr[0].length() - 1))
				+ Double.valueOf(locArr[1].substring(0, locArr[1].length() - 1)) / 60
				+ Double.valueOf(locArr[2].substring(0, locArr[2].length() - 1)) / 3600;
		return location;
	}

}
